package com.yanqu.road.logic.cross;

import com.yanqu.road.entity.rank.cross.CrossActivityTeamRank;
import com.yanqu.road.logic.bussiness.rank.CrossGeneralActivityRankBusiness;
import com.yanqu.road.logic.bussiness.rank.RankBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.helper.RankListHelper;
import com.yanqu.road.logic.pb.CrossRankPb;
import com.yanqu.road.logic.sort.CrossTeamRankSort;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.rank.RankProto;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class CrossActivityTeamRankListModel {

    private int rankNum = GameConfig.RANK_DEFAULT_MAX_COUNT;

    private boolean isNeedSync = false;

    private boolean isUpdate = false;

    private CrossTeamRankSort crossTeamRankSort;

    private boolean needUpdateDB;           //是否需要把数据入库

    private LinkedList<CrossActivityTeamRank> rankList;

    private int activityId;
    private int groupId;

    private int conditionType;

    private CrossActivityProto.TeamRankListMsg.Builder rankListMsg;

    private int rankCapacity = calcRankCapacity(GameConfig.RANK_DEFAULT_MAX_COUNT);

    public CrossActivityTeamRankListModel(int activityId, int groupId, int conditionType, boolean needUpdateDB) {
        this.activityId = activityId;
        this.groupId = groupId;
        this.conditionType = conditionType;
        rankList = new LinkedList<>();
        this.needUpdateDB = needUpdateDB;
        rankListMsg = CrossActivityProto.TeamRankListMsg.newBuilder();
        crossTeamRankSort = new CrossTeamRankSort();
    }

    public int getGroupId() {
        return groupId;
    }

    public int getRankNum() {
        return rankNum;
    }

    public void setRankNum(int rankNum) {
        this.rankNum = rankNum;
        rankCapacity = calcRankCapacity(rankNum);
    }

    public boolean isUpdate() {
        return isUpdate;
    }

    public void setUpdate(boolean update) {
        isUpdate = update;
    }

    public boolean isNeedSync() {
        return isNeedSync;
    }

    public void setNeedSync(boolean needSync) {
        isNeedSync = needSync;
    }

    public CrossTeamRankSort getCrossTeamRankSort() {
        return crossTeamRankSort;
    }

    public void setCrossTeamRankSort(CrossTeamRankSort crossTeamRankSort) {
        this.crossTeamRankSort = crossTeamRankSort;
    }

    public boolean isNeedUpdateDB() {
        return needUpdateDB;
    }

    public void setNeedUpdateDB(boolean needUpdateDB) {
        this.needUpdateDB = needUpdateDB;
    }

    public LinkedList<CrossActivityTeamRank> getRankList() {
        return rankList;
    }

    public void setRankList(LinkedList<CrossActivityTeamRank> teamRankList) {
        synchronized (this) {
            rankList = new LinkedList<>(teamRankList);
            RankListHelper.checkTeamActivityRankListValid(rankList);       //校验一下数组
            rankList.sort(crossTeamRankSort);
            while (rankList.size() > rankCapacity){
                rankList.removeLast();
            }
            initRankMsg();
        }
    }

    private void initRankMsg() {
        ArrayList<CrossActivityTeamRank> arrayList = new ArrayList<>(rankList);
        List<CrossActivityTeamRank> tempList = new ArrayList<>();
        int size = 0;
        for (CrossActivityTeamRank rank : arrayList) {
            tempList.add(rank);
            size++;
            if (size >= rankNum) {
                break;
            }
        }
        rankListMsg = CrossRankPb.parseTeamRankListMsg(tempList);
    }

    public int getMyRank(int teamId, long serverId) {
        int rank = -1;
        int index = 0;
        synchronized (this) {
            for(CrossActivityTeamRank teamRank : rankList){
                if(teamRank.getTeamId() == teamId && teamRank.getServerId() == serverId){
                    rank = index + 1;
                    break;
                }
                index++;
            }
        }
        if(rank > rankNum){
            rank = -1;
        }
        return rank;
    }

    public CrossActivityTeamRank getMyRankInfo(int teamId, long serverId) {
        synchronized (this) {
            for(CrossActivityTeamRank teamRank : rankList){
                if(teamRank.getTeamId() == teamId && teamRank.getServerId() == serverId){
                    return teamRank;
                }
            }
        }
        return null;
    }


    /**
     * 添加到排行
     */
    public void rankChange(CrossActivityTeamRank crossTeamRank) {
        boolean isChange = false;
        synchronized (this) {
            int size = rankList.size();
            CrossActivityTeamRank teamRank = getMyRankInfo(crossTeamRank.getTeamId(), crossTeamRank.getServerId());
            if(null != teamRank){
                teamRank.setTeamBaseInfo(crossTeamRank.getTeamBaseInfo());
                if(0 != teamRank.getValue().compareTo(crossTeamRank.getValue())){
                    teamRank.setLastUpdateTime(System.currentTimeMillis() / 1000);
                }
                teamRank.setValue(crossTeamRank.getValue());
                isChange = true;
            }else {
                if (size < rankCapacity) {
                    CrossActivityTeamRank rank = crossTeamRank.clone();
                    rank.setLastUpdateTime(System.currentTimeMillis() / 1000);
                    rankList.add(rank);
                    isChange = true;
                } else if (crossTeamRankSort.compare(crossTeamRank, rankList.getLast()) < 0) {
                    CrossActivityTeamRank rank = crossTeamRank.clone();
                    rank.setLastUpdateTime(System.currentTimeMillis() / 1000);
                    rankList.removeLast();
                    rankList.add(rank);
                    isChange = true;
                }
            }
            if(isChange) {
                rankList.sort(crossTeamRankSort);
                initRankMsg();
                isNeedSync = true;
                isUpdate = true;
            }
        }
    }

    public void save() {
        if(needUpdateDB) {
            LinkedList<CrossActivityTeamRank> newRankList;
            synchronized (this) {
                if (!isUpdate) {
                    return;
                }
                newRankList = new LinkedList<>(rankList);
                isUpdate = false;
            }
            RankBussiness.updateCrossActivityTeamRankList(newRankList, conditionType);
        }
    }



    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public int getConditionType() {
        return conditionType;
    }

    public void setConditionType(int conditionType) {
        this.conditionType = conditionType;
    }

    public CrossActivityProto.TeamRankListMsg.Builder getRankListMsg() {
        return rankListMsg;
    }

    public void setRankListMsg(CrossActivityProto.TeamRankListMsg.Builder rankListMsg) {
        this.rankListMsg = rankListMsg;
    }

    private int calcRankCapacity(int rankNum) {
        int value = rankNum / 2 + rankNum;
        if (value <= 200) {
            return 200;
        }
        return value;
    }

}
